﻿//===============================================================================
// File: LinkHelper.cs
// Author:Veiller Hu
// CLR Version：4.0.30319.1
// Created Date:  2011/3/10 10:36:01 PM 
// Summary:  
//===============================================================================
//
// Copyright (c) East China Information Technology Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//===============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GPMS.BLL
{
    using GPMS.Model;
    using System.Web.Mvc;
    using System.Web;
    using System.Web.Security;
    public static class LinkHelper
    {

        public static List<Link> GetList(ExtendedGPMSDb db)
        {

            return db.Links
                .Where(l => l.IsDeleted == false)
                .OrderBy(l => l.LinkType)
                .ThenBy(l => l.LinkName)
                .ToList();
        }
        /// <summary>
        /// 获取链接数据
        /// </summary>
        /// <param name="linkType">链接类型</param>
        /// <param name="db">EF</param>
        /// <param name="Url">UrlHelper</param>
        /// <param name="Request">HttpRequestBase</param>
        /// <returns></returns>
        public static dynamic GetListDynamic(LinkType linkType,
            ExtendedGPMSDb db, UrlHelper Url, HttpRequestBase Request)
        {
            //链接类型
            string linkTypeStr = linkType.ToString();
            //登陆用户id
            Guid userID = AccountHelper.GetLoginUserID(Request);
            //获得Controller信息
            var controllers = ControllerHelper.GetList(db)
                .ToDictionary(c => c.ControllerID);
            //获得Action信息
            var actions = db.ActionInfos
                .Where(a => a.IsDeleted == false && a.Links.Count > 0)
                .ToDictionary(a => a.ActionID);
            //获得有权限的链接信息
            var links = db.Links
                .Where(l => l.IsDeleted == false && l.LinkType == linkTypeStr &&
                    l.ActionInfos.Roles.Any(r => r.Users.Any(u => u.UserID == userID)))
                .OrderBy(l => l.LinkType)
                .ThenBy(l => l.LinkName)
                .ToList();
            //用于图标地址信息
            Dictionary<Guid, List<string>> iconAndUrl = new Dictionary<Guid, List<string>>();
            //生成图标信息
            links.ForEach(l =>
                {
                    if (l.FileID.HasValue)
                    {
                        iconAndUrl.Add(l.LinkID, new List<string>()
                {
                    Url.Action("Download", "File", new { id = l.FileID }),
                    Url.Action(actions[l.ActionID].ActionName, 
                    controllers[actions[l.ActionID].ControllerID].ControllerName).Replace("Controller","")
                });
                    }
                    else
                    {
                        iconAndUrl.Add(l.LinkID, new List<string>()
                {
                    string.Empty,
                    Url.Action(actions[l.ActionID].ActionName, 
                    controllers[actions[l.ActionID].ControllerID].ControllerName).Replace("Controller","")
                });
                    }

                });
            // 生成返回信息
            var entities = links
                .Select(l => new
                {
                    LinkName = l.LinkName,
                    Fullscreen = l.Fullscreen,
                    Width = l.Width,
                    Height = l.Height,
                    Maxable = l.Maxable,
                    Minable = l.Minable,
                    Resizeable = l.Resizeable,
                    ActionID = l.ActionID,
                    FileID = l.FileID,
                    LinkID = l.LinkID,
                    Icon = iconAndUrl[l.LinkID][0],
                    Url = iconAndUrl[l.LinkID][1]
                }).ToList();
            return entities;
        }

        public static Link Get(Guid id, ExtendedGPMSDb db)
        {
            return db.Links.FirstOrDefault(l => l.LinkID == id && l.IsDeleted == false);
        }

        public static Guid Add(string type, Link link, ExtendedGPMSDb db)
        {
            link.LinkType = type;
            return Add(link, db);
        }

        public static Guid Add(Link link, ExtendedGPMSDb db)
        {
            if (link.LinkID == Guid.Empty)
            {
                link.LinkID = Guid.NewGuid();
            }
            link.IsDeleted = false;
            link.CreateDate = DateTime.Now;
            db.Links.AddObject(link);
            db.SaveChanges();
            return link.LinkID;
        }

        public static void Delete(Guid id, ExtendedGPMSDb db)
        {
            Link link = db.Links.FirstOrDefault(l => l.LinkID == id);
            link.IsDeleted = true;
            db.SaveChanges();
        }
    }
}
